package com.catbad.services.impl;

import com.catbad.domain.*;
import com.catbad.mapper.*;
import com.catbad.services.FilmServices;
import com.catbad.vo.FilmPageVO;
import com.catbad.vo.MoreFilmPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FilmServicesImpl implements FilmServices {
    @Autowired
    FilmMapper filmMapper;
    @Autowired
    LanguageMapper languageMapper;
    @Autowired
    FilmtypeMapper filmtypeMapper;
    @Autowired
    CountryMapper countryMapper;
    @Autowired
    AreaMapper areaMapper;
    @Override
    public FilmPageVO findPageForFilm(String filmName,String languageCode,String typeCode,String countryCode, Integer pageNO, Integer rows) {
        //存储查询条件的Map
        Map<String,Object> params = new HashMap<>();
        //检查筛选正确性并赋值，
        //第一次都为null,如果存入Map，在jsp中得不到值，会为空，会报错，所以为null值，赋初始值“”
        if(filmName==null){
            params.put("filmName","");
        }else {
            params.put("filmName",filmName);
        }
        if(languageCode==null){
            params.put("languageCode","");
        }else {
            params.put("languageCode",languageCode);
        }
        if(typeCode==null){
            params.put("typeCode","");
        }else {
            params.put("typeCode",typeCode);
        }
        if(countryCode==null){
            params.put("countryCode","");
        }else {
            params.put("countryCode",countryCode);
        }
        //根据条件查询总数，便于后面分页的总页数计算
        Long total = filmMapper.total(params);
        //检查分页元素
        //第一次都为null，赋初值
        if(pageNO==null){
            pageNO = 1;
        }
        //由于可能通过输入页面进入的，页面不正确，进行处理
        //页面下限为1
        if(pageNO<1){
            pageNO = 1;
        }
        if(rows==null){
            rows = 4;
        }
        //计算最大页
        //maxPgae可能等于0,
        int maxPage = (int)(total%rows==0? total/rows:(total/4+1));
        maxPage = Math.max(1,maxPage);
        //页面上限处理，保证页面最小等于1，最大不大于最大页面
        if(pageNO>maxPage){
            pageNO = maxPage;
        }
        int pageStart = (pageNO-1)*rows;
        int pageLength = rows;
        params.put("pageStart",pageStart);
        params.put("pageLength",pageLength);
        //数据库查询结果返回
        List<Map<String,Object>> filmList= filmMapper.findPageForFilm(params);
        List<Filmtype> typeList = filmtypeMapper.selectAll();
        List<Country> countryList = countryMapper.selectAll();
        List<Language> languageList = languageMapper.selectAll();
        //code转字符串
        formatFilmMap(filmList,typeList,countryList,languageList);
        FilmPageVO filmPageVO = new FilmPageVO();
        //下拉列表查询条件
        filmPageVO.setCountryList(countryList);
        filmPageVO.setLanguageList(languageList);
        filmPageVO.setTypeList(typeList);
        filmPageVO.setParams(params);
        //影片信息
        filmPageVO.setFilmList(filmList);
        //分页元素
        filmPageVO.setPageNo(pageNO);
        filmPageVO.setMaxPage(maxPage);
        filmPageVO.setRows(rows);
        filmPageVO.setTotal(total);
        return filmPageVO;
    }

    @Override
    public int saveFilm(Film film) {
        return filmMapper.saveFilm(film);
    }

    @Override
    public int deleteFilm(Long fid) {
        return filmMapper.deleteByPrimaryKey(fid);
    }

    @Override
    public Film getFilmById(Long fid) {
        return filmMapper.selectByPrimaryKey(fid);
    }

    @Override
    public int setFilm(Film film,String path) {
        //如果需要修改图片，删除原来的照片
        Film f1 =filmMapper.selectByPrimaryKey(film.getFilmId());
        if(film.getFilmImg()!=null && path!=null){
            String imgName = f1.getFilmImg();
            String imgPath = path+"img\\"+imgName;
            File fi = new File(imgPath);
            if(fi.exists()){
                fi.delete();
            }
        }else {
            film.setFilmImg(f1.getFilmImg());
        }
        film.setFilmDeleteFlag("1");
        return filmMapper.updateByPrimaryKey(film);
    }

    @Override
    public List<Map<String, Object>> getAllFilm() {
        //数据库查询结果返回
        List<Map<String,Object>> filmList= filmMapper.getAllFilm();
        List<Filmtype> typeList = filmtypeMapper.selectAll();
        List<Country> countryList = countryMapper.selectAll();
        List<Language> languageList = languageMapper.selectAll();
        formatFilmMap(filmList,typeList,countryList,languageList);
        return filmList;
    }

    @Override
    public List<Map<String,Object>> getFilmByName(String filmName) {
        if(filmName!=null) {
            return filmMapper.getFilmByName(filmName);
        }
        return null;
    }

    @Override
    public Map<String,Object> getFilmMapById(Long fid) {
        Map<String,Object> film = filmMapper.getFilmMapById(fid);
        List<Filmtype> typeList = filmtypeMapper.selectAll();
        List<Country> countryList = countryMapper.selectAll();
        List<Language> languageList = languageMapper.selectAll();
        List<Area> areaList = areaMapper.selectArea1();
        formatFilmMap(film,typeList,countryList,languageList);
        film.put("areaList",areaList);
        return film;
    }

    @Override
    public MoreFilmPageVO getMoreFilmPage(String filmName, Integer pageNO) {
        Map<String,Object> params = new HashMap<>();
        MoreFilmPageVO moreFilmPageVO = new MoreFilmPageVO();
        if(filmName==null){
            params.put("filmName","");
        }else {
            params.put("filmName",filmName);
        }
        //第一次进入pageNO可能为null,也可能用skip选择页面进入，可能是错误页面
        if(pageNO==null){
            pageNO=1;
        }
        if(pageNO<1){
            pageNO=1;
        }
        //得到总行数
        Long total = filmMapper.moreTotal(params);
        int maxPage = (int)(total%8==0?total/8:total/8+1);
        maxPage = Math.max(1,maxPage);
        if(pageNO>maxPage){
            pageNO = maxPage;
        }
        int start = (pageNO-1)*8;
        int length = 8;
        params.put("start",start);
        params.put("length",length);
        List<Film> filmList = filmMapper.findMoreFilmPage(params);
        moreFilmPageVO.setFilmList(filmList);
        moreFilmPageVO.setPageNo(pageNO);
        moreFilmPageVO.setParams(params);
        moreFilmPageVO.setRows(8);
        moreFilmPageVO.setTotal(total);
        moreFilmPageVO.setMaxPage(maxPage);
        return moreFilmPageVO;
    }

    //将数据的code转换成字符串的辅助.为filmList
    private void formatFilmMap(List<Map<String,Object>> filmList,List<Filmtype> typeList,List<Country> countryList,List<Language> languageList){
        //处理得到的filmList里面只有code和code串
        //国家是单独的code,
        // 语言和类型是多字符串，split拆分，Set存储查找是否存在对应code，循环转换转换为汉字字符串
        for(Map<String,Object> film:filmList){
           formatFilmMap(film,typeList,countryList,languageList);
        }
    }
    //将一个mapfilm的code转换成字符串
    private void formatFilmMap(Map<String,Object> film,List<Filmtype> typeList,List<Country> countryList,List<Language> languageList){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String ccode = (String)film.get("country");
            //处理时间格式问题
            film.put("play_time", format.format((Date) film.get("play_time")));
            //国家code对应的字符串
            for(Country country:countryList){
                if(country.getCountryCode().equals(ccode)){
                    film.put("countryName",country.getCountryName());
                    break;
                }
            }
            //语言code对应的字符串
            String l1 = (String)film.get("language");
            String[] l2 = l1.split(",");
            Set<String> l3 = new HashSet<>();
            for(int i=0;i<l2.length;i++){
                l3.add(l2[i]);
            }
            String lname = "";
            for(Language language:languageList){
                if(l3.contains(language.getLcode())){
                    lname = lname+language.getLname()+",";
                }
            }
            lname = lname.substring(0,lname.length()-1);
            film.put("languageName",lname);
            //类型code对应的字符串
            String t1 = (String)film.get("types");
            String[] t2 = t1.split(",");
            Set<String> t3 = new HashSet<>();
            for(int i=0;i<t2.length;i++){
                t3.add(t2[i]);
            }
            String tname = "";
            for(Filmtype filmtype:typeList){
                if(t3.contains(filmtype.getFtcode())){
                    tname = tname+filmtype.getFtname()+",";
                }
            }
            tname = tname.substring(0,tname.length()-1);
            film.put("filmTypeName",tname);
    }

}
